import unittest
from node import Node
from tree import Tree
from generatetree import GenerateTree

class TestGenerateTrees(unittest.TestCase):

    def setUp(self):
        self.root = Node("root", style=Node.ROOT)
        self.nodeA = Node("A", self.root, style=Node.NORM)
        self.nodeB = Node("B", self.nodeA, style=Node.NORM)
        self.nodeC = Node("C", self.nodeA, style=Node.NORM)
        #self.nodeE = Node("E", self.nodeC, style=Node.NORM)
        self.root.setChild1(self.nodeA)
        self.nodeA.setChild1(self.nodeB)
        self.nodeA.setChild2(self.nodeC)
        #self.nodeC.setChild1(self.nodeE)
        self.tree1 = Tree(self.root)
        self.generator = GenerateTree(100, 20, 20, 4000, 100)
        root = Node("root", style=Node.ROOT)
        nodeA = Node("A", root, style=Node.NORM)
        nodeB = Node("B", nodeA, style=Node.NORM)
        nodeC = Node("C", nodeA, style=Node.NORM)
        nodeE = Node("E", nodeC, style=Node.NORM)
        root.setChild1(nodeA)
        nodeA.setChild1(nodeB)
        nodeA.setChild2(nodeC)
        nodeC.setChild1(nodeE)
        self.tree2 = Tree(root)
        self.generator2 = GenerateTree(100, 20, 20, 4000, 100)

    def test_ordered_binary(self):
        tree = self.generator.getTreeOrdered(20)
        count = 0
        for item in tree.iterator(tree.getRoot()):
            count = count + 1
        #print count
        self.assertTrue(count-1 == 20)
        return

    def test_unordered_binary(self):
        tree = self.generator.getTreeUnordered(20)
        count = 0
        for item in tree.iterator(tree.getRoot()):
            count = count + 1
        self.assertTrue(count-1 == 20)
        return

    def test_get_changed_tree(self):
        self.generator.getOrdered()
        tree = self.generator.getChangedTree(self.tree2)
        count = 0
        for item in tree.iterator(tree.getRoot()):
            count = count + 1
        #print count
        return

    def test_unordered_tree(self):
        tree = self.generator.getTreeUnordered(20)
        count = 0
        for item in tree.iterator(tree.getRoot()):
            count = count + 1
        self.assertTrue(count-1 == 20)
        return

    def test_switch_nodes(self):
        self.generator.switchNodes(self.nodeB, self.nodeC)
        self.assertTrue(self.nodeA.getChild1().getID() == self.nodeC.getID())
        self.assertTrue(self.nodeA.getChild2().getID() == self.nodeB.getID())

    def test_relabel_node(self):
        self.generator.relabelNode(self.nodeB, "Test")
        self.assertTrue(self.nodeA.getChild1().getLabel() == "Test")

    def test_insert_node(self):
        nodeD = Node("D", style=Node.NORM)
        self.generator.insertNode(self.nodeA, nodeD)
        if self.nodeA.getChild1().getID() == nodeD.getID():
            self.assertTrue(self.nodeA.getChild1().getID() == nodeD.getID())
            self.assertTrue(nodeD.getChild1().getID() == self.nodeB.getID())
        elif self.nodeA.getChild2().getID() == nodeD.getID():
            self.assertTrue(self.nodeA.getChild2().getID() == nodeD.getID())
            self.assertTrue(nodeD.getChild2().getID() == self.nodeC.getID())

    def test_insert_unique(self):
        return

    def test_find_unique(self):
        return

    def test_delete_node(self):
        root = self.tree2.getRoot()
        nodeA = root.getChild1()
        nodeB = nodeA.getChild1()
        nodeC = nodeA.getChild2()
        nodeE = nodeC.getChild1()
        self.generator2.deleteNode(nodeE)
        self.assertTrue(nodeC.getChild1() is None)
        self.generator2.deleteNode(nodeA)
        self.assertTrue(root.getChild1() is nodeB)

    def test_rotate_node(self):
        return

    def test_get_node(self):
        return

    def test_get_operations(self):
        return
